package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dao.CategoryMapper;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.CustomException;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
        implements CategoryService {

    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    
    @Override
    public void removeById(Long id) {
        // 1 判断菜品是否包含该分类
        LambdaQueryWrapper<Dish> dishWrapper = new LambdaQueryWrapper<>();
        dishWrapper.eq(Dish::getCategoryId, id);
        int countByDish = dishService.count(dishWrapper);
        if (countByDish > 0) {
            throw new CustomException("当前菜品分类关联了菜品，无法删除！！！");
        }
        LambdaQueryWrapper<Setmeal> setmealWrapper = new LambdaQueryWrapper<>();
        setmealWrapper.eq(Setmeal::getCategoryId,id);
        int countBySetmeal = setmealService.count(setmealWrapper);
        if (countBySetmeal > 0){
            throw  new CustomException("当前菜品分类关联了套餐，无法删除！！！");
        }
        super.removeById(id);
    }
}
